package cn.linshu.security.jwt.advice;

import cn.linshu.security.jwt.dto.CommonResult;
import cn.linshu.security.jwt.dto.ResultCode;
import cn.linshu.security.jwt.exception.BusinessException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

import java.util.Objects;

/**
 * -*- coding: utf-8 -*-
 *
 * @Author linnin
 * @Name springboot3-labs-master
 * @Time 2025/2/4 21:53
 * @Note
 * @For
 * @Out Copyright© 2025 linnin
 */

/* coding: utf-8 */

@Slf4j
@RestControllerAdvice
public class GlobalExceptionHandler {

    @ExceptionHandler(BusinessException.class)
    public CommonResult handleBusinessException(BusinessException e) {
        log.error("BusinessException: {}", e.toString());
        // 返回自定义的错误信息和状态码
        return CommonResult.failed(Objects.requireNonNull(ResultCode.getByCode(e.getCode())));
    }

    @ExceptionHandler(Exception.class)
    public CommonResult handleException(Exception e) {
        log.error("Exception: {}", e.getMessage());
        // 返回自定义的错误信息和状态码
        return CommonResult.failed(e.getMessage() + " 错误类:" + e.getClass().getName());
    }

}